package com.facebook.imagepipeline.producers;

import bolts.Continuation;
import bolts.Task;
import com.facebook.cache.common.CacheKey;
import com.facebook.common.internal.ImmutableMap;
import com.facebook.common.internal.VisibleForTesting;
import com.facebook.imagepipeline.cache.BufferedDiskCache;
import com.facebook.imagepipeline.cache.CacheKeyFactory;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequest.CacheChoice;
import com.facebook.imagepipeline.request.ImageRequest.RequestLevel;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicBoolean;

public class DiskCacheProducer
  implements Producer<EncodedImage>
{

  @VisibleForTesting
  static final String PRODUCER_NAME = "DiskCacheProducer";

  @VisibleForTesting
  static final String VALUE_FOUND = "cached_value_found";
  private final CacheKeyFactory mCacheKeyFactory;
  private final boolean mChooseCacheByImageSize;
  private final BufferedDiskCache mDefaultBufferedDiskCache;
  private final int mForceSmallCacheThresholdBytes;
  private final Producer<EncodedImage> mInputProducer;
  private final BufferedDiskCache mSmallImageBufferedDiskCache;

  public DiskCacheProducer(BufferedDiskCache paramBufferedDiskCache1, BufferedDiskCache paramBufferedDiskCache2, CacheKeyFactory paramCacheKeyFactory, Producer<EncodedImage> paramProducer, int paramInt)
  {
    this.mDefaultBufferedDiskCache = paramBufferedDiskCache1;
    this.mSmallImageBufferedDiskCache = paramBufferedDiskCache2;
    this.mCacheKeyFactory = paramCacheKeyFactory;
    this.mInputProducer = paramProducer;
    this.mForceSmallCacheThresholdBytes = paramInt;
    if (paramInt > 0);
    for (boolean bool = true; ; bool = false)
    {
      this.mChooseCacheByImageSize = bool;
      return;
    }
  }

  @VisibleForTesting
  static Map<String, String> getExtraMap(ProducerListener paramProducerListener, String paramString, boolean paramBoolean)
  {
    if (!paramProducerListener.requiresExtraMap(paramString))
      return null;
    return ImmutableMap.of("cached_value_found", String.valueOf(paramBoolean));
  }

  private static boolean isTaskCancelled(Task<?> paramTask)
  {
    return (paramTask.isCancelled()) || ((paramTask.isFaulted()) && ((paramTask.getError() instanceof CancellationException)));
  }

  private void maybeStartInputProducer(Consumer<EncodedImage> paramConsumer1, Consumer<EncodedImage> paramConsumer2, ProducerContext paramProducerContext)
  {
    if (paramProducerContext.getLowestPermittedRequestLevel().getValue() >= ImageRequest.RequestLevel.DISK_CACHE.getValue())
    {
      paramConsumer1.onNewResult(null, true);
      return;
    }
    this.mInputProducer.produceResults(paramConsumer2, paramProducerContext);
  }

  private Continuation<EncodedImage, Void> onFinishDiskReads(final Consumer<EncodedImage> paramConsumer, final BufferedDiskCache paramBufferedDiskCache, final CacheKey paramCacheKey, final ProducerContext paramProducerContext)
  {
    final String str = paramProducerContext.getId();
    return new Continuation()
    {
      public Void then(Task<EncodedImage> paramAnonymousTask)
        throws Exception
      {
        if (DiskCacheProducer.isTaskCancelled(paramAnonymousTask))
        {
          this.val$listener.onProducerFinishWithCancellation(str, "DiskCacheProducer", null);
          paramConsumer.onCancellation();
          return null;
        }
        if (paramAnonymousTask.isFaulted())
        {
          this.val$listener.onProducerFinishWithFailure(str, "DiskCacheProducer", paramAnonymousTask.getError(), null);
          DiskCacheProducer.this.maybeStartInputProducer(paramConsumer, new DiskCacheProducer.DiskCacheConsumer(DiskCacheProducer.this, paramConsumer, paramBufferedDiskCache, paramCacheKey, null), paramProducerContext);
          return null;
        }
        EncodedImage localEncodedImage = (EncodedImage)paramAnonymousTask.getResult();
        if (localEncodedImage != null)
        {
          this.val$listener.onProducerFinishWithSuccess(str, "DiskCacheProducer", DiskCacheProducer.getExtraMap(this.val$listener, str, true));
          paramConsumer.onProgressUpdate(1.0F);
          paramConsumer.onNewResult(localEncodedImage, true);
          localEncodedImage.close();
          return null;
        }
        this.val$listener.onProducerFinishWithSuccess(str, "DiskCacheProducer", DiskCacheProducer.getExtraMap(this.val$listener, str, false));
        DiskCacheProducer.this.maybeStartInputProducer(paramConsumer, new DiskCacheProducer.DiskCacheConsumer(DiskCacheProducer.this, paramConsumer, paramBufferedDiskCache, paramCacheKey, null), paramProducerContext);
        return null;
      }
    };
  }

  private void subscribeTaskForRequestCancellation(final AtomicBoolean paramAtomicBoolean, ProducerContext paramProducerContext)
  {
    paramProducerContext.addCallbacks(new BaseProducerContextCallbacks()
    {
      public void onCancellationRequested()
      {
        paramAtomicBoolean.set(true);
      }
    });
  }

  public void produceResults(Consumer<EncodedImage> paramConsumer, ProducerContext paramProducerContext)
  {
    ImageRequest localImageRequest = paramProducerContext.getImageRequest();
    if (!localImageRequest.isDiskCacheEnabled())
    {
      maybeStartInputProducer(paramConsumer, paramConsumer, paramProducerContext);
      return;
    }
    paramProducerContext.getListener().onProducerStart(paramProducerContext.getId(), "DiskCacheProducer");
    final CacheKey localCacheKey = this.mCacheKeyFactory.getEncodedCacheKey(localImageRequest, paramProducerContext.getCallerContext());
    int i;
    BufferedDiskCache localBufferedDiskCache1;
    label83: final AtomicBoolean localAtomicBoolean;
    BufferedDiskCache localBufferedDiskCache2;
    final BufferedDiskCache localBufferedDiskCache3;
    if (localImageRequest.getCacheChoice() == ImageRequest.CacheChoice.SMALL)
    {
      i = 1;
      if (i == 0)
        break label202;
      localBufferedDiskCache1 = this.mSmallImageBufferedDiskCache;
      localAtomicBoolean = new AtomicBoolean(false);
      if (!this.mChooseCacheByImageSize)
        break label226;
      boolean bool1 = this.mSmallImageBufferedDiskCache.containsSync(localCacheKey);
      boolean bool2 = this.mDefaultBufferedDiskCache.containsSync(localCacheKey);
      if ((!bool1) && (bool2))
        break label211;
      localBufferedDiskCache2 = this.mSmallImageBufferedDiskCache;
      localBufferedDiskCache3 = this.mDefaultBufferedDiskCache;
    }
    label144: label202: label211: label226: for (Task localTask = localBufferedDiskCache2.get(localCacheKey, localAtomicBoolean).continueWithTask(new Continuation()
    {
      public Task<EncodedImage> then(Task<EncodedImage> paramAnonymousTask)
        throws Exception
      {
        if ((DiskCacheProducer.isTaskCancelled(paramAnonymousTask)) || ((!paramAnonymousTask.isFaulted()) && (paramAnonymousTask.getResult() != null)))
          return paramAnonymousTask;
        return localBufferedDiskCache3.get(localCacheKey, localAtomicBoolean);
      }
    }); ; localTask = localBufferedDiskCache1.get(localCacheKey, localAtomicBoolean))
    {
      localTask.continueWith(onFinishDiskReads(paramConsumer, localBufferedDiskCache1, localCacheKey, paramProducerContext));
      subscribeTaskForRequestCancellation(localAtomicBoolean, paramProducerContext);
      return;
      i = 0;
      break;
      localBufferedDiskCache1 = this.mDefaultBufferedDiskCache;
      break label83;
      localBufferedDiskCache2 = this.mDefaultBufferedDiskCache;
      localBufferedDiskCache3 = this.mSmallImageBufferedDiskCache;
      break label144;
    }
  }

  private class DiskCacheConsumer extends DelegatingConsumer<EncodedImage, EncodedImage>
  {
    private final BufferedDiskCache mCache;
    private final CacheKey mCacheKey;

    private DiskCacheConsumer(BufferedDiskCache paramCacheKey, CacheKey arg3)
    {
      super();
      Object localObject1;
      this.mCache = localObject1;
      Object localObject2;
      this.mCacheKey = localObject2;
    }

    public void onNewResultImpl(EncodedImage paramEncodedImage, boolean paramBoolean)
    {
      if ((paramEncodedImage != null) && (paramBoolean))
      {
        if (!DiskCacheProducer.this.mChooseCacheByImageSize)
          break label83;
        int i = paramEncodedImage.getSize();
        if ((i <= 0) || (i >= DiskCacheProducer.this.mForceSmallCacheThresholdBytes))
          break label65;
        DiskCacheProducer.this.mSmallImageBufferedDiskCache.put(this.mCacheKey, paramEncodedImage);
      }
      while (true)
      {
        getConsumer().onNewResult(paramEncodedImage, paramBoolean);
        return;
        label65: DiskCacheProducer.this.mDefaultBufferedDiskCache.put(this.mCacheKey, paramEncodedImage);
        continue;
        label83: this.mCache.put(this.mCacheKey, paramEncodedImage);
      }
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.producers.DiskCacheProducer
 * JD-Core Version:    0.6.2
 */